package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


// 购物车
@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    // 添加购物车
    @Override
    public Cart add(Cart cartParam) {
        //查询购物车是否存在(之前添加过没)
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId, UserHolder.get().getId());//用户的id在线程里
        qw.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());//菜品id
        qw.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId()); //套餐id
        Cart cart = cartMapper.selectOne(qw);
        //判断
        if (cart == null) {//不存在新增
            cart = cartParam;
            cart.setUserId(UserHolder.get().getId());//补全用户信息
            cart.setNumber(1); //补全数量
            cart.setCreateTime(new Date());//补全添加购物车时间
            cartMapper.insert(cart);
        } else {// 存在，数量+1，更新数据库
            cart.setNumber(cart.getNumber() + 1);
            cartMapper.updateById(cart);
        }
        //返回
        return cart;
    }


    //查询购物车
    @Override
    public List<Cart> list() {
        //构建条件 //根据用户id查询购物车
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId, UserHolder.get().getId());
        //查询
        List<Cart> cartList = cartMapper.selectList(qw);
        return cartList;
    }

 /*   //修改购物车减少数量
    @Override
    public Cart updateByList(Map<String, Long> map) {
        //取出dishId菜品
        Long dishId = map.get("dishId");
        //构建条件
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        //根据dishId菜品和用户查询需要修改的购物车
        qw.eq(Cart::getDishId,dishId);
        qw.eq(Cart::getUserId,UserHolder.get().getId());
        Cart cart = cartMapper.selectOne(qw);
        //操作购物车对数量-1
        cart.setNumber(cart.getNumber()-1);
        //判断修改后的数量
        if (cart.getNumber()==0){
            //删除购物车记录 根据id删除
            cartMapper.deleteById(cart.getId());
        }else if (cart.getNumber()>0){
            //更新购物车记录
          *//*  Cart cart1 = new Cart();
            cart1.setNumber(cart1.getNumber()-1);*//*
            cartMapper.updateById(cart);
        }
        //返回购物车对象
        return cart;

    }*/
// 修改购物车
 @Override
 public Cart sub(Cart cartParam) {
     // 1.先查询购物车是否有此项数据
     //构建条件
     LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
     wrapper.eq(Cart::getUserId, UserHolder.get().getId()); // 查id
     wrapper.eq(cartParam.getDishId() != null, Cart::getDishId, cartParam.getDishId());
     wrapper.eq(cartParam.getSetmealId() != null, Cart::getSetmealId, cartParam.getSetmealId());
     Cart cart = cartMapper.selectOne(wrapper);
     cart.setNumber(cart.getNumber() - 1);
     if (cart.getNumber() > 0) {
         cartMapper.updateById(cart);
     } else if (cart.getNumber() ==0){
         cartMapper.deleteById(cart);
     }
     return cart;
 }


    // 清空购物车
    @Override
    public void clean() {
     //构建条件
        //根据用户id删除
        LambdaQueryWrapper<Cart> qw = new LambdaQueryWrapper<>();
        qw.eq(Cart::getUserId,UserHolder.get().getId());
                //删除
        cartMapper.delete(qw);
    }


}